package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.FaqTypeService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.masterData.FaqTypeDelDto;
import com.ccys.db.dto.masterData.FaqTypeFindListDto;
import com.ccys.db.dto.masterData.FaqTypeSaveDto;
import com.ccys.db.entity.TblAccount;
import com.ccys.db.entity.TblFaq;
import com.ccys.db.entity.TblFaqType;
import com.ccys.db.mapper.TblFaqMapper;
import com.ccys.db.mapper.TblFaqTypeMapper;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: FaqType实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 14:55
 **/

@Service
public class FaqTypeServiceImpl extends BaseServiceImpl<TblFaqType> implements FaqTypeService {

    @Resource
    private TblFaqTypeMapper tblFaqTypeMapper;

    @Resource
    private TblFaqMapper tblFaqMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveFaqType(FaqTypeSaveDto dto) {
        //需要校验：国家-集团-名称 并且未删除是否存在
        if (ObjectUtil.isNotEmpty(dto.getName()) && ObjectUtil.isNotEmpty(dto.getGroupId()) && ObjectUtil.isNotEmpty(dto.getCountryId())) {
            //准备查询条件
            Example example = new Example(TblFaqType.class);
            example.createCriteria()
                    .andEqualTo("countryId", dto.getCountryId())
                    .andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("name", dto.getName())
                    .andEqualTo("delFlag", 0);
            //判断是否传入ID - 传入ID属于修改 - 需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                example.createCriteria().andNotEqualTo("id", dto.getId());
            }
            //开始查询并检查如果存在则返回错误信息
            if (ObjectUtil.isNotEmpty(tblFaqTypeMapper.selectOneByExample(example))) {
                return ResponseResult.error("The FaqType name under the current country group already exists!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }
        if(StringUtil.isEmpty(dto.getId())) {
            dto.setStatus(1);
        }
        //以上校验完成 - 开始新增/修改
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteFaqType(FaqTypeDelDto dto) {
        //校验是否传入ID
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //查询当前Type下是否存在未删除的Faq - 准备查询条件
        Example example = new Example(TblFaq.class);
        example.createCriteria()
                .andEqualTo("typeId", dto.getId())
                .andEqualTo("delFlag", 0);
        //查询数量 - 如果大于0则返回错误信息
        int count = tblFaqMapper.selectCountByExample(example);
        if (count > 0) {
            return ResponseResult.error("Currently, there are still " + count + " data entries in FaqType, deletion is prohibited!");
        }
        //以上如果都没问题 - 开始软删除当前FaqType
        TblFaqType type = new TblFaqType();
        type.setId(dto.getId());
        type.setDelFlag(1);
        if (tblFaqTypeMapper.updateByPrimaryKeySelective(type) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findListFaqType(FaqTypeFindListDto dto) {
        //检查字段值不在范围则赋值null
        if (ObjectUtil.isEmpty(dto.getCountryId())){
            //如果未传入国家ID
            TblAccount account = UserUtil.getCurLoginAccount();
            if (ObjectUtil.isEmpty(account)){
                return ResponseResult.error("Not Logged In");
            }
            dto.setAccountId(account.getId());
        }
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        return ResponseResult.success(tblFaqTypeMapper.findListFaqType(dto));
    }
}
